﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using System.Web.UI;

namespace Cyberwolf.Modules.PWCAPAdmin.Components
{
    public static class Utilities
    {
        public static readonly DateTime MinDateTime = new DateTime(1753, 1, 1);

        public static string FormatFileSize(long byteSize)
        {
            if (byteSize >= 1024 * 1024)
            {
                return ((decimal)byteSize / 1024M / 1024M).ToString("n1") + " MB";
            }
            else if (byteSize >= 1024)
            {
                return ((decimal)byteSize / 1024M).ToString("n1") + " KB";
            }
            else
            {
                return byteSize + " B";
            }
        }

        /// <summary>
        /// Method to get description text for templates
        /// </summary>
        public static string GetDescriptionText(string description)
        {
            if (description.Length < 116)
            {
                return description;
            }
            else
            {
                return (description.Substring(0, 113) + "...");
            }
        }

        public static string GetDisplayDate(DateTime date)
        {
            if (MinDateTime == date)
            {
                return string.Empty;
            }
            else
            {
                return date.ToShortDateString();
            }
        }

        public static string GetDisplayDateTime(DateTime date)
        {
            if (MinDateTime == date)
            {
                return string.Empty;
            }
            else
            {
                return date.ToString("MM/dd/yyyy h:mm tt");
            }
        }

        public static string GetDisplayTime(DateTime date)
        {
            if (MinDateTime == date)
            {
                return string.Empty;
            }
            else
            {
                return date.ToString("h:mm tt");
            }
        }

        public static bool SettingsOK(Hashtable Settings)
        {
            if (string.IsNullOrEmpty((string)Settings["SERVER_NAME"])) return false;
            if (string.IsNullOrEmpty((string)Settings["SERVICE_URL"])) return false;

            return true;
        }

        public static string EncodeXML(string inputString)
        {
            return "<![CDATA[" + inputString + "]]>";
        }

        /// <summary>
        /// Formats the provided XML so it's indented and humanly-readable.
        /// </summary>
        /// <param name="inputXml">The input XML to format.</param>
        public static string FormatXml(string inputXml)
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml(inputXml);

            StringBuilder builder = new StringBuilder();
            using (XmlTextWriter writer = new XmlTextWriter(new StringWriter(builder)))
            {
                writer.Formatting = Formatting.Indented;
                document.Save(writer);
            }

            return builder.ToString();
        }

        public static T FindControl<T>(this Control ctl, string id) where T : Control
        {
            foreach (Control child in ctl.Controls)
            {
                if (child is T)
                {
                    if (child.ID == id) return (T)child;
                }
            }
            return default(T);
        }
    }
}
